package cn.mw.cmdb.view;

import cn.mw.cmdb.entity.PropertyInfo;
import cn.mw.cmdb.entity.PropertyType;
import cn.mw.cmdb.entity.PropertyTypeManage;
import cn.mw.cmdb.entity.PropertyTypeRef;
import cn.mw.cmdb.param.ModelPropertyParam;
import cn.mw.cmdb.util.CMDBTool;
import org.apache.commons.collections.map.ListOrderedMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.mw.cmdb.entity.PropertyTypeString.ValueKey;
import static cn.mw.cmdb.util.ListSortUtil.sortListByTypeOrder;

public class InstanceInpuFieldsViewConverter implements ViewConvert<ModelPropertyParam, InstanceInputFieldView> {
    public static final String key = "viewMap";
    public static final String propertyListKey = "propertyListKey";

    @Override
    public void init(ViewConvertContext context) throws Exception {
        Map<String, List> catologMap = new ListOrderedMap();
        context.put(key, catologMap);
    }

    @Override
    public void finish(ViewConvertContext context, List<InstanceInputFieldView> list) throws Exception {
        Map<String, List> catologMap = (Map) context.get(key);
        Object propertyList = context.get(propertyListKey);
        List ret = new ArrayList();
        for (Map.Entry<String, List> itemMap : catologMap.entrySet()) {
            InstanceInpuFieldsView instanceInpuFieldsView = new InstanceInpuFieldsView();
            instanceInpuFieldsView.setType(itemMap.getKey());
            List<InstanceInputFieldView> value = itemMap.getValue();
            if (itemMap != null && itemMap.getValue() instanceof List) {
                value = (List<InstanceInputFieldView>) itemMap.getValue();
                if (propertyList != null && propertyList instanceof List) {
                    List<String> propertyListInfo = (List<String>) propertyList;
                    sortListByTypeOrder(value, CMDBTool.getFieldName(InstanceInputFieldView::getId), propertyListInfo);
                }
            }
            instanceInpuFieldsView.setData(value);
            ret.add(instanceInpuFieldsView);
        }
        context.put(InstanceInpuFieldsView.Key, ret);
    }

    @Override
    public InstanceInputFieldView convert(ModelPropertyParam param, ViewConvertContext context) {
        Map<String, List> catologMap = (Map) context.get(key);
        List list = catologMap.get(param.getPropertyCatologName());
        if (null == list) {
            list = new ArrayList();
            catologMap.put(param.getPropertyCatologName(), list);
        }

        InstanceInputFieldView view = new InstanceInputFieldView();
        view.setId(param.getId());
        view.setLabel(param.getName());

        PropertyTypeRef propertyTypeRef = param.getPropertyTypeRef();
        view.setPropertyType(propertyTypeRef.getTypeId());
        Map<String, Object> contextMap = propertyTypeRef.getContext();
        if (contextMap != null) {
            view.setDefaultValue(contextMap.get(ValueKey));
        }

        PropertyType propertyType = PropertyTypeManage.getPropertyType(propertyTypeRef.getTypeId());
        Object defaultInputValue = propertyType.getDefaultInputValue(null, propertyTypeRef.getContext());
        view.setValue(defaultInputValue);

        Map viewContext = new HashMap();
        viewContext.put(CMDBTool.getFieldName(PropertyInfo::getId), param.getId());
        viewContext.put(CMDBTool.getFieldName(PropertyInfo::getName), param.getName());
        if (null != propertyTypeRef.getContext() && !propertyTypeRef.getContext().isEmpty()) {
            viewContext.putAll(propertyTypeRef.getContext());
        }

        view.setContext(viewContext);
        view.setFieldAuthTypes(param.getFieldAuthTypes());
        list.add(view);
        return view;
    }
}
